
/**
 * 简单的加密解密
 * @author romejiang
 */
public class SimpleEncode {
 
    def String publickey = "XoGHIkl[hinJYjpmKLMFcde]^_`ab\\szSZDrBCUVETqtuAfgPNOxyQRwWv";
    def int init = 65
    
    def static main(args){
        def se = new SimpleEncode();
//        (65..122).each{
//            print new String( [it as byte] as byte[]  )
//        }
        def code = se.encode("abcdefghijklmnopqrstuvwxyz");
        def decode =  se.decode(code);
        println code
        assert "abcdefghijklmnopqrstuvwxyz" == decode

        code = se.encode("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        decode =  se.decode(code);
        println code
        assert "ABCDEFGHIJKLMNOPQRSTUVWXYZ" == decode   
        
    }
    /**
     * 对 String 进行加密
     * @param str 要加密的数据
     * @return 返回加密后的  string
     */
     def encode(String str) {
        byte[] b = str.getBytes()
        StringBuffer sb = new StringBuffer()
        for(int i = 0;i < b.length; i++){  
            sb.append( publickey[b[i]-65] )
        }
        return sb.toString()
     } 
    /**
     * 对 string 进行解密
     * @param 要解密的数据
     * @return 返回加密后的 String
     */
     def decode(String str) {
        byte[] b = str.getBytes()
     
        StringBuffer sb = new StringBuffer()
        str.each{
            sb.append(   new String( [publickey.indexOf(it)+65 as byte] as byte[] ))
        }
        return sb.toString()
     }
}

